﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace GrayParrot.Web.Controls
{
    public enum MessageTypes
    {
        Success = 0,
        Info,
        Warning,
        Error
    }

    public class ReturnMessage
    {
        public string Message { get; set; }
        public MessageTypes MessageType { get; set; }

        public ReturnMessage()
        {
        }
    }

    /// <summary>
    /// A full result, as understood by jQuery DataTables.
    /// </summary>
    /// <typeparam name="T">The data type of each row.</typeparam>
    public class DTResult<T>
    {
        /// <summary>
        /// The draw counter that this object is a response to - from the draw parameter sent as part of the data request.
        /// Note that it is strongly recommended for security reasons that you cast this parameter to an integer, rather than simply echoing back to the client what it sent in the draw parameter, in order to prevent Cross Site Scripting (XSS) attacks.
        /// </summary>
        public int draw { get; set; }

        /// <summary>
        /// Total records, before filtering (i.e. the total number of records in the database)
        /// </summary>
        public int recordsTotal { get; set; }

        /// <summary>
        /// Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned for this page of data).
        /// </summary>
        public int recordsFiltered { get; set; }

        /// <summary>
        /// The data to be displayed in the table.
        /// This is an array of data source objects, one for each row, which will be used by DataTables.
        /// Note that this parameter's name can be changed using the ajaxDT option's dataSrc property.
        /// </summary>
        public List<T> data { get; set; }

        public ReturnMessage Message { get; set; }

        /// <summary>
        /// Return an empty instance for showing no data available and not blocking the datatables plugin.
        /// </summary>
        /// <returns></returns>
        public static DTResult<T> Empty(string message)
        {
            DTResult<T> retValue = new DTResult<T>();

            retValue.draw = 0;
            retValue.data = new List<T>();
            retValue.recordsFiltered = 0;
            retValue.recordsTotal = 0;
            retValue.Message = new ReturnMessage() { Message=message, MessageType=MessageTypes.Warning };

            return retValue;
        }


        /// <summary>
        /// Adds the filter drop down data.
        /// </summary>
        /// <typeparam name="F">the returning type of Func predicate. Is inferred from the predicate</typeparam>
        /// <param name="filterColumnPosition">The filter column position.</param>
        /// <param name="action">Predicate that return the item to insert in the dropdown filter</param>
        /// <returns>a JSON object containig configuration data for Datatables.net</returns>
        /// <remarks>
        /// This method is usuful when for the datatables.net is defined a yadcf filter of type select.
        /// With this method is possible to define the items that represent the options of the dropdown filter.
        /// This items is always in the form of an array...
        /// </remarks>
        public JObject AddFilterDropDownData<F>(int filterColumnPosition, Func<F> action)
        {
            JObject retValue = null;

            retValue = JObject.FromObject(this);

            F actionResult = action.Invoke();

            retValue.Add(string.Format("yadcf_data_{0}", filterColumnPosition), JToken.FromObject(actionResult));

            return retValue;
        }
    }
}
